#!/bin/bash
#chkconfig:345 61 61
#description: Nginx简易安装脚本，版本以及nginx安装模块可修改。
#auther: Wang Chen Chen

# nginx 安装目录
nginx_install_dir="/usr/local/nginx"

# nginx 版本
nginx_version="1.20.1"

InstallNginx(){
	# 编译 nginx 需要依赖的软件
	diApp="wget gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel"

	# 获得当前 CentOS 系统发行版本号
	osVersion=`cat /etc/redhat-release | awk '{match($0,"release ")
	 print substr($0,RSTART+RLENGTH)}' | awk -F '.' '{print $1}'`

  	if [ $osVersion = 8 ];then
		dnf install -y $diApp
	else
		yum install -y $diApp
	fi

	mkdir -p ${nginx_install_dir}

	# Download nginx
	cd /tmp

	useradd -M -s /sbin/nologin nginx >/dev/null 2>&1

	wget -c "https://nginx.org/download/nginx-${nginx_version}.tar.gz"

	echo "==================== Start Installing Nginx ===================="

	tar -zxvf nginx-${nginx_version}.tar.gz >/dev/null 2>&1

	cd nginx-${nginx_version}
	echo "==================== Start Compile Nginx "$nginx_version"===================="
	./configure --user=nobody --group=nobody \
	--prefix=${nginx_install_dir} \
	--with-http_stub_status_module \
	--with-http_gzip_static_module \
	--with-http_realip_module \
	--with-http_sub_module \
	--with-http_ssl_module >/dev/null 2>&1

	make >/dev/null 2>&1
	make install >/dev/null 2>&1

  	# nginx.service configuration
	cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=${nginx_install_dir}/sbin/nginx
ExecReload=${nginx_install_dir}/sbin/nginx -s reload
ExecStop=${nginx_install_dir}/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

EOF
	chmod 600 /usr/lib/systemd/system/nginx.service
	systemctl daemon-reload
	systemctl enable nginx
	systemctl start nginx
	echo "创建 nginx 开机启动完成"

	# 检查防火墙状态
	firewallStatus=`ps -ef|grep firewalld|grep -v grep|wc -l`
 	if [ $firewallStatus = 1 ];then
		echo "防火墙 开启 80 和 443 端口"
		firewall-cmd --permanent --zone=public --add-service=http
		firewall-cmd --permanent --zone=public --add-service=https
		firewall-cmd --reload
  	else
    		echo "Firewall Service is stop..."
  	fi

	echo "==================== Nginx Installing Successfully ===================="
	echo "Nginx: Installing location : ${nginx_install_dir} "
	echo "systemctl start nginx"
	echo "systemctl stop nginx"
	echo "systemctl status nginx"
	echo "systemctl restart nginx"
	echo "systemctl reload nginx"
}

UninstallNginx(){
	echo "==================== Uninstall Nginx ===================="
	# 先暂停nginx服务
	systemctl stop nginx
	systemctl disable nginx
	# yum卸载nginx
	yum remove nginx -y
	# 删除nginx相关文件夹
	rm -rf ${nginx_install_dir} \
	/usr/lib/systemd/system/nginx.service \
	/etc/nginx \
	/etc/init.d/nginx \
	/usr/sbin/nginx
}

# 卸载
function uninstall()
{
  UninstallNginx
}

# 安装
function install()
{
  UninstallNginx
  InstallNginx
}


# 第一个参数 install|uninstall|i|u 中的一个
if [ "$1" = "" ];
then
    echo -e "\033[0;31m 未输入操作名 \033[0m  \033[0;34m {install|uninstall|i|u} \033[0m"
    exit 1
fi

case $1 in
    install)
    install;;
    i)
    install;;
    uninstall)
    uninstall;;
    u)
    uninstall;;
    *)

    echo -e "\033[0;31m Usage: \033[0m  \033[0;34m sh  $0  {install|uninstall|i|u} \033[0m
\033[0;31m Example: \033[0m
      \033[0;33m sh  $0  start \033[0m"
esac


